package com.yunshan.welfare;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import static com.yunshan.framework.datasource.DynamicDataSourceContextHolder.log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yunshan.common.annotation.Log;
import com.yunshan.common.core.controller.BaseController;
import com.yunshan.common.core.domain.AjaxResult;
import com.yunshan.common.enums.BusinessType;
import com.yunshan.welfare.domain.WelfareGift;
import com.yunshan.welfare.service.IWelfareGiftService;
import com.yunshan.common.utils.poi.ExcelUtil;
import com.yunshan.common.core.page.TableDataInfo;

/**
 * 礼包管理Controller
 *
 * @author yunshan
 * @date 2025-01-20
 */
@Api(tags = "礼包管理")
@RestController
@RequestMapping("/welfare/gift")
public class WelfareGiftController extends BaseController
{
    @Autowired
    private IWelfareGiftService welfareGiftService;

    /**
     * 查询礼包管理列表
     */
    @ApiOperation("查询礼包管理列表")
    @PreAuthorize("@ss.hasPermi('welfare:gift:list')")
    @GetMapping("/list")
    public TableDataInfo list(WelfareGift welfareGift)
    {
        startPage();
        List<WelfareGift> list = welfareGiftService.selectWelfareGiftList(welfareGift);
        return getDataTable(list);
    }

    /**
     * 查询礼包管理列表
     */
    @ApiOperation("查询礼包管理列表（不分页）")
    @PreAuthorize("@ss.hasPermi('welfare:gift:list')")
    @GetMapping("/query")
    public AjaxResult query(WelfareGift welfareGift)
    {
        List<WelfareGift> list = welfareGiftService.selectWelfareGiftList(welfareGift);
        return success(list);
    }

    /**
     * 导出礼包管理列表
     */
    @ApiOperation("导出礼包管理列表")
    @PreAuthorize("@ss.hasPermi('welfare:gift:export')")
    @Log(title = "礼包管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WelfareGift welfareGift)
    {
        List<WelfareGift> list = welfareGiftService.selectWelfareGiftList(welfareGift);
        ExcelUtil<WelfareGift> util = new ExcelUtil<WelfareGift>(WelfareGift.class);
        util.exportExcel(response, list, "礼包管理数据");
    }

    /**
     * 获取礼包管理详细信息
     */
    @ApiOperation("获取礼包管理详细信息")
    @PreAuthorize("@ss.hasPermi('welfare:gift:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(welfareGiftService.selectWelfareGiftById(id));
    }

    /**
     * 新增礼包管理
     */
    @ApiOperation("新增礼包管理")
    @PreAuthorize("@ss.hasPermi('welfare:gift:add')")
    @Log(title = "礼包管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WelfareGift welfareGift)
    {
        try {
            return toAjax(welfareGiftService.insertWelfareGift(welfareGift));
        }catch (Exception e){
            log.error("新增礼包失败：{}",e.getMessage());
            return AjaxResult.error("新增礼包失败，请稍后重试");
        }
    }

    /**
     * 修改礼包管理
     */
    @ApiOperation("修改礼包管理")
    @PreAuthorize("@ss.hasPermi('welfare:gift:edit')")
    @Log(title = "礼包管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WelfareGift welfareGift)
    {
        return toAjax(welfareGiftService.updateWelfareGift(welfareGift));
    }

    /**
     * 删除礼包管理
     */
    @ApiOperation(value = "删除礼包管理")
    @PreAuthorize("@ss.hasPermi('welfare:gift:remove')")
    @Log(title = "礼包管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(welfareGiftService.deleteWelfareGiftByIds(ids));
    }


}
